package Commands;

import java.util.Map;

import javax.naming.*;

import org.json.simple.*;

import Helpers.ApplicationLogger;

public final class ClientCommands {
	
	 private static final class Nested
     {
         static final ClientCommands _instance = new ClientCommands();
     }
	 
	 private ClientCommands()
	 {
		 
	 }

     public static ClientCommands getInstance()
     {
           return Nested._instance;
     }
     
     public ClientCommandResult ExecuteCommand(String commandName, String jsnSerializedCommand, Map<String, String> serviceParameters)
     {
        ClientCommandResult result = new ClientCommandResult(); 
        JSONObject jObj = (JSONObject) JSONValue.parse(jsnSerializedCommand); 
        ApplicationLogger.getInstance().LOG.info("Command parsed");
        
        Context ctx = null;
		try {
			ctx = new InitialContext();
		} catch (NamingException e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage());

		}
		
        IClientCommandExecuter command = null;
			try {
				command = (IClientCommandExecuter)ctx.lookup(commandName);
			} catch (NamingException e) {
				ApplicationLogger.getInstance().LOG.error(e.getMessage());
			}
			
        command.FillFromJSON(jObj);
        command.setServiceParameters(serviceParameters);
        command.Execute();

        result = command.GetResult();

        return result;
     }
}
